package com.young.common.util;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * AES加密工具类
 * @author wangjiyu@imdada.cn
 * @create 2018/12/20
 */
public class AESUtil {

    public static final String KEY_ALGORITHM = "AES";//AES算法
    public static final int KEY_LENGTH = 128;//秘钥长度
    public static final String CHARSET = "UTF-8";//编码方式

    /**
     * 生成秘钥
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);//AES秘钥生成器
        keyGen.init(KEY_LENGTH);//设置秘钥长度
        SecretKey secretKey = keyGen.generateKey();//生成秘钥
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    /**
     * 加密
     * @param text 明文
     * @param key 秘钥
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String encript(String text, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        //秘钥
        SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);//加解密对象
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);//加密模式
        byte[] res = cipher.doFinal(text.getBytes(CHARSET));//加密
        return Base64.getEncoder().encodeToString(res);
    }

    /**
     * 解密
     * @param ciphertext 密文
     * @param key 秘钥
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String decript(String ciphertext, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        //秘钥
        SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);//加解密对象
        cipher.init(Cipher.DECRYPT_MODE, secretKey);//解密模式
        byte[] res = cipher.doFinal(Base64.getDecoder().decode(ciphertext));//解密
        return new String(res, CHARSET);
    }

    /*public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
        String key = "Al4q36FkLSoj5BWfYcQ3Rg==";//generateKey();
        System.out.println("秘钥: " + key);
        String text = "JD_368033261cc";
        System.out.println("原文: " + text);
        String s = encript(text, key);//加密
        System.out.println("加密后: " + s);
        String s2 = decript(s, key);//解密
        System.out.println("解密后: " + s2);

    }*/
}
